package com.baosight.sczx.zm.cc.service;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cc.domain.ZMCCProduceproblem;

/**
 * 
 * <p>Title: ServiceZMCC00.java</p>
 * <p>Description: 生产问题录入</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 郭淑芳
 * @version 1.0
 * <pre>Histroy:
 *          2016年9月27日 下午1:49:44  郭淑芳  Create
 *</pre>
 */
public class ServiceZMCC00 extends ServiceEPBase {
	/**
	 * 
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始进入方法</p>
	 * <p>return value: ServiceZMCC00</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月27日下午1:49:53 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");// 如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: query</p>
	 * <p>Description: 查询方法</p>
	 * <p>return value: ServiceZMCC00</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月27日下午1:50:07 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdMiningId  = UserSession.getInSessionProperty("miningCode").toString();
		StaticTools.setQueryParameters(inInfo, "fdMiningId", fdMiningId, true);
		EiInfo outInfo = super.query(inInfo, "ZMCCProduceproblem.query", new ZMCCProduceproblem());

		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cp.problemType", true));// 问题类型
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.cp.problemStatus", new String[]{"0", "1", "2", "3", "4", "5", "6"}, new String[]{"草拟", "未处理", "未查收", "未整改", "未核查", "关闭", "作废"}, true));//问题状态
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cp.problemLevel", true));// 问题级别
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.cp.isCheck", new String[] { "否", "是" }, new String[] { "否", "是" }, true));// 是否重复问题
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cp.problemOrigin"));// 问题来源
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", true, UserSession.getInSessionProperty("miningCode").toString()));// 责任部门

		outInfo.set("fdCreatorId", UserSession.getUserId());// 登录人的默认值
		outInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
		outInfo.set("fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));// 单位的默认值
		outInfo.set("fdCreateDepartment", UserSession.getInSessionProperty("orgName"));// 单位的默认值
		outInfo.set("fdMiningId", fdMiningId);// 矿ID的默认值

		return outInfo;
	}
	
	
	/**
	 * 
	 * <p>Function: insert</p>
	 * <p>Description: 新增方法</p>
	 * <p>return value: ServiceZMCC00</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月27日下午1:54:12 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		String fdMiningId = (String) UserSession.getInSessionProperty("miningCode");// 矿ID的默认值
		EiInfo out = new EiInfo();
		ZMCCProduceproblem _dom = new ZMCCProduceproblem();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();

		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			String problemTimeF = _dom.getFdProblemTimeF();
			String deadlineTimeF = _dom.getFdDeadlineTimeF();
			
			_dom.setFdProblemDate(this.getDay(problemTimeF)); // 发生日期
			_dom.setFdProblemTime(this.getTime(problemTimeF));	// 发生时间
			_dom.setFdDeadlineDate(this.getDay(deadlineTimeF));	// 解决日期
			_dom.setFdDeadlineTime(this.getTime(deadlineTimeF));	//解决时间
			_dom.setFdCreateTime(StaticTools.getDateTime());
			_dom.setFdMiningId(fdMiningId);// 矿井id
			_dom.setFdCreateUserId(UserSession.getUserId().toString());
			_dom.setFdCreateUser(UserSession.getInSessionProperty("UserCname") == null ? "" : UserSession.getInSessionProperty("UserCname").toString());
			_dom.setFdSubmitorDepartment(Department.getInstance().getDepartmentNameById(_dom.getFdSubmitorDepartmentId()));
			_dom.setFdBusinessDepartment(Department.getInstance().getDepartmentNameById(_dom.getFdBusinessDepartmentId()));
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CC_PRODUCEPROBLEM"));
			
			this.dao.insert("ZMCCProduceproblem.insert", _dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}

	/**
	 * 
	 * <p>Function: update</p>
	 * <p>Description: 修改方法</p>
	 * <p>return value: ServiceZMCC00</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月27日下午1:55:02 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCCProduceproblem _dom = new ZMCCProduceproblem();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));

			String problemTimeF = _dom.getFdProblemTimeF();
			String deadlineTimeF = _dom.getFdDeadlineTimeF();
			
			_dom.setFdProblemDate(this.getDay(problemTimeF)); // 发生日期
			_dom.setFdProblemTime(this.getTime(problemTimeF));	// 发生时间
			_dom.setFdDeadlineDate(this.getDay(deadlineTimeF));	// 解决日期
			_dom.setFdDeadlineTime(this.getTime(deadlineTimeF));	//解决时间

			this.dao.update("ZMCCProduceproblem.update", _dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;

	}
	
	/**
	 * 
	 * <p>Function: delete</p>
	 * <p>Description: 删除方法</p>
	 * <p>return value: ServiceZMCC00</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月27日下午1:55:14 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo outInfo = null;
		for (int i = 0; i < inInfo.getBlock(EiConstant.resultBlock).getRowCount(); i++) {
			EiInfo _temp = super.delete(inInfo, "ZMCCProduceproblem.delete");
			outInfo = this.query(inInfo);
			outInfo.setMsg(_temp.getMsg());
			outInfo.setDetailMsg(_temp.getDetailMsg());
		}
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: report</p>
	 * <p>Description: 上报方法</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月27日下午1:55:31 郭淑芳
	 * </p>
	 *
	 */
	public EiInfo report(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCCProduceproblem _dom = new ZMCCProduceproblem();
		EiBlock eiBlock = inInfo.getBlock("result");

		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			this.dao.update("ZMCCProduceproblem.report", _dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}
	
	/**
	 * 
	 * <p>Function: getFirDay</p>
	 * <p>Description: 根据传入的年份、月份，获取当月第一天日期</p>
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年7月28日下午4:54:39 郭淑芳
	 * </p>
	 *
	 */
	private String getFirDay(String yearStr, String monthStr) {
		if (monthStr.trim().length() < 2) {
			monthStr = yearStr.trim() + "-0" + monthStr.trim() + "-01";
		} else {
			monthStr = yearStr.trim() + "-" + monthStr.trim() + "-01";
		}
		return monthStr;
	}
	
	/**
	 * 
	 * <p>Function: getDay</p>
	 * <p>Description: 依据传入的字符串（yyyy-MM-dd hh:mm:ss）获取日期（yyyy-MM-dd）</p>
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月27日下午2:14:57 郭淑芳
	 * </p>
	 *
	 */
	private String getDay(String timeStr) {
		String[] tt = timeStr.split(" ");
		return tt[0];
	}
	
	/**
	 * 
	 * <p>Function: getTime</p>
	 * <p>Description: 依据传入的字符串（yyyy-MM-dd hh:mm:ss）获取时间（hh:mm:ss）</p>
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月27日下午2:18:19 郭淑芳
	 * </p>
	 *
	 */
	private String getTime(String timeStr) {
		String[] tt = timeStr.split(" ");
		return tt[1];
	}
}
